@extends('base')

@section('content')
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form layui-form-pane">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">角色名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="请输入搜索关键字" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="role-query"><i class="layui-icon layui-icon-search"></i>查询</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="role-table" lay-filter="role-table"></table>
    </div>
</div>

<script type="text/html" id="role-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>
</script>

<script type="text/html" id="role-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="accounts-list">
    @{{#for (var key in d.accounts) { }}
    <span>[@{{ d.accounts[key]['account'] }}]</span>
    @{{# } }}
</script>

<script type="text/html" id="role-createTime">
  @{{ layui.util.toDateString(d.create_time, 'yyyy-MM-dd HH:mm:ss') }}
</script>
@endsection

@section('js')
<script>
    layui.use(['table','form','jquery'],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let cols = [
            [
                {title: '排序', field: 'role_sort', align:'center', width:80},
                {title: '角色名', field: 'role_name', align:'center'},
                {title: '管理员', field: 'accounts', align:'center', templet:'#accounts-list'},
                {title: '描述', field: 'role_intro', align:'center'},
                {title: '创建时间', field: 'create_time', align: 'center', templet: '#role-createTime'},
                {title: '操作', toolbar: '#role-bar', align:'center', width:195}
            ]
        ]

        table.render({
            elem: '#role-table',
            url: '/role/list',
            page: true ,
            cols: cols ,
            skin: 'line',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(role-table)', function(obj){
            if(obj.event === 'remove'){
                window.remove(obj);
            } else if(obj.event === 'edit'){
                window.edit(obj);
            }
        });

        table.on('toolbar(role-table)', function(obj){
            if(obj.event === 'add'){
                window.add();
            } else if(obj.event === 'refresh'){
                window.refresh();
            }
        });

        form.on('submit(role-query)', function(data){
            table.reload('role-table',{where:data.field})
            return false;
        });

        window.add = function(){
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['100%', '100%'],
                content: '/role/add'
            });
        }

        window.edit = function(obj){
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['100%', '100%'],
                content: '/role/edit/' + obj.data['id']
            });
        }

        window.remove = function(obj){
            layer.confirm('确定要删除该角色', {icon: 3, title:'提示'}, function(index){
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/role/delete/"+obj.data['id'],
                    dataType:'json',
                    type:'delete',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success:function(result){
                        layer.close(loading);
                        if(result.status){
                            layer.msg(result.msg,{icon:1,time:1000},function(){
                                obj.del();
                            });
                        }else{
                            layer.msg(result.msg,{icon:2,time:1000});
                        }
                    }
                })
            });
        }

        window.refresh = function(){
            table.reload('role-table');
        }
    })
</script>
@endsection

